<?php

namespace App\Http\Controllers;
use Hash;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Config;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserPostRequest;
use App\Http\Requests\UserEditPostReques;
class UserController extends Controller
{
	//添加前 显示
    public function getAdd()
    {
    	//解析模板======添加页面
    	return view('admin.user.add');
    }
    //用户添加操作
    public function postInsert(UserPostRequest $request)
    {
    	//提取需要的数据
        $data = $request->only(['username','password','phone','status','email']);
        //token随机
        $data['token'] = str_random(50);
        //hash 密码重新加密
        $data['password'] = Hash::make($data['password']);
        //添加事件
        $data['addtime'] = Carbon::now()->toDateTimeString();
        //判断是否上传图片
        if($request->hasFile('pic')){
            //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            //随机文件名
            $rand = time().rand(1,999999);
            //文件移动
            $request->file('pic')->move('public/uploads/',$rand.'.'.$ext);
            $data['pic'] = trim(Config::get('/public/uploads/').$rand.'.'.$ext,'.');
            //dd($data['pic']);
            //缩放 
            $img = Image::make('public/uploads/'.$data['pic'])->resize(240, 250);
            //把缩放图片保存
            $img->save('public/uploads/'.'s_'.$data['pic']);
            // $data['pic']
        }
        //dd($data);
        //执行数据库插入  及判断
        if(DB::table('hm_users')->insert($data)){
            //成功返回
            return redirect('/admin/user/index')->with('success','添加成功');
        }else{
            //失败返回
            return back()->with('error','添加失败');
        }
    }
    //用户列表显示
    public function getIndex(Request $request)
    {
        //获取搜索分页 显示条数
        $num = $request->input('num',3);
        //判断使用使用搜索
        if($request->input('keywords')){
              //有关键字
              $data = DB::table('hm_users')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
             //每页搜索全部
            $data = DB::table('hm_users')->paginate($num);
        }
        
        //状态栏的几种状态
        $status_list = ['1'=>'未激活','2'=>'普通会员','3'=>'后台管理','4'=>'禁用'];
    	//解析模板=========用户列表页
    	return view('admin.user.index',['data'=>$data,'status_list'=>$status_list,'request'=>$request->all()]);
    }
    //用户删除 操作
    public function getDel($id)
    {
        //获取提交id的数据
        $data = DB::table('hm_users')->where('id','=',$id)->first();
        //拼接图片路径
        $pic = 'public/uploads/'.$data->pic;
        $spic = 'public/uploads/'.'s_'.$data->pic;
        //判断执行删除
        if(file_exists($pic)){
            unlink($pic);
            unlink($spic);
        }
       //执行删除并判断
        if(DB::table('hm_users')->where('id',$id)->delete()){
            //删除成功
            return redirect('/admin/user/index')->with('success','删除成功');
        }else{
            //删除失败
            return back()->with('error','删除失败');
        }
    }
    //用户修改前 显示
    public function getEdit($id)
    {
        //根据id查询 =========要修改的信息
        $data = DB::table('hm_users')->where('id',$id)->first();
        //解析模板============ edit 信息页面
        return view('admin.user.Edit',['data'=>$data]);
    }
    //执行用户修改
    public function postUpdate(UserEditPostReques $request)
    {
        //dd($request->all());
        //接收数据username status email phone
        $data = $request->only(['username','status','email','phone']);
        $old = 0;//图片修改标准
        //判断是否上传图片
        if($request->hasFile('pic')){
            //获取后缀名
            $ext = $request->file('pic')->getClientOriginalExtension();
            //随机文件名
            $rand = time().rand(1,999999);
            //文件移动
            $request->file('pic')->move('public/uploads/',$rand.'.'.$ext);
            $data['pic'] = trim(Config::get('/public/uploads/').$rand.'.'.$ext,'.');
            //dd($data['pic']);
            //缩放 
            $img = Image::make('public/uploads/'.$data['pic'])->resize(50, 50);
            //把缩放图片保存
            $img->save('public/uploads/'.'s_'.$data['pic']);
             //修改标志
            $old = 1;
         }
        //dd($data);
        //判断及修该
        if(DB::table('hm_users')->where('id',$request->input('id'))->update($data)){
            //修改成功
            if($old==1){
           
                $pic = 'public/uploads/'.$request->input('oldpic');
                $spic = 'public/uploads/'.'s_'.$request->input('oldpic');
                //判断执行删除
                if(file_exists($pic)){
                    unlink($pic);
                    unlink($spic);
                }
             }
            return redirect('/admin/user/index')->with('success','修改成功');
        }else{
            //修改失败
            return back()->with('error','修改失败');
        }

    }

  
}
